package com.stellar.auth.controller.miniApp.authUser;

import com.google.common.base.Preconditions;

import com.stellar.auth.controller.admin.authUser.vo.AuthUserRequestVO;
import com.stellar.auth.controller.admin.authUser.vo.AuthUserResponseVO;
import com.stellar.auth.controller.miniApp.authUser.vo.MiniAppLoginResponseVO;
import com.stellar.auth.domain.authUser.AuthUserDomainService;
import com.stellar.web.core.bean.Result;
import com.stellar.web.core.constants.AuthUserConstants;
import com.stellar.web.core.validator.ValidatorRole;
import com.stellar.wechat.core.auth.WxAuthService;
import com.stellar.wechat.core.common.exception.WxApiException;
import com.stellar.wechat.core.entity.WxSessionInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 系统用户信息表控制器
 *
 * @author Jqchan
 * @version 1.0
 * @date 2025-05-16 18:05:29
 */
@RequestMapping("/api-miniApp/authUser")
@RestController("miniAppAuthUserController")
@Slf4j
public class AuthUserController {

    @Resource
    private AuthUserDomainService authUserDomainService;

    @Resource
    private WxAuthService wxAuthService;

    /**
     * 修改系统用户信息表
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN, AuthUserConstants.NORMAL_USER})
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthUserRequestVO requestVO) {
        try {
            if (log.isInfoEnabled()) {
                Preconditions.checkNotNull(requestVO, "请求体不能为空");
                Preconditions.checkNotNull(requestVO.getId(), "用户Id不能为空");
                Preconditions.checkNotNull(requestVO.getNickName(), "微信昵称不能为空");
                Preconditions.checkNotNull(requestVO.getOpenid(), "OpenId不能为空");
                Preconditions.checkNotNull(requestVO.getSchoolId(), "学校Id不能为空");
            }
            return Result.success(authUserDomainService.update(requestVO));
        } catch (IllegalArgumentException iae) {
            log.error("AuthUserController.update.error:{}", iae.getMessage());
            return Result.fail(iae.getMessage());
        }
    }

    /**
     * 获取系统用户信息表详情
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN, AuthUserConstants.NORMAL_USER})
    @GetMapping("/get/{id}")
    public Result<AuthUserResponseVO> get(@PathVariable("id") Long id) {
        return Result.success(authUserDomainService.get(id));
    }


    /**
     * 身份认证 - 用户首次进入小程序时，进行身份认证
     *
     * @param authUserRequestVO
     * @return
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN, AuthUserConstants.NORMAL_USER})
    @PostMapping("/identification")
    public Result<Boolean> identification(@RequestBody AuthUserRequestVO authUserRequestVO) {
        try {
            if (log.isInfoEnabled()) {
                Preconditions.checkNotNull(authUserRequestVO, "请求体不能为空");
                Preconditions.checkNotNull(authUserRequestVO.getId(), "用户Id不能为空");
                Preconditions.checkNotNull(authUserRequestVO.getOpenid(), "OpenId不能为空");
                Preconditions.checkNotNull(authUserRequestVO.getSchoolId(), "学校不能为空");
                Preconditions.checkNotNull(authUserRequestVO.getGrade(), "年级不能为空");
            }
            Boolean res = authUserDomainService.identification(authUserRequestVO);
            if (!res) return Result.fail(res);
            return Result.success(res);
        } catch (IllegalArgumentException iae) {
            log.error("AuthUserController.identification.error:{}", iae.getMessage());
            return Result.fail(iae.getMessage());
        }
    }


    /**
     * 小程序登录
     *
     * @param code
     * @return
     */
    @GetMapping("/miniAppLogin")
    public Result<MiniAppLoginResponseVO> miniAppLogin(@RequestParam("code") String code) {
        try {
            WxSessionInfo wxSessionInfo = wxAuthService.code2Session(code);
            MiniAppLoginResponseVO miniAppLoginResponseVO = authUserDomainService.miniAppLogin(wxSessionInfo);
            return Result.success(miniAppLoginResponseVO);
        } catch (WxApiException wae) {
            log.error("AuthUserController.miniAppLogin.error:{},errcode:{},errmgs:{}", wae.getMessage(), wae.getErrcode(), wae.getErrmsg());
            return Result.fail(wae.getErrcode(), wae.getErrmsg());
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail(e.getMessage());
        }
    }

}
